<?php
class My_DataBase_Mapper_Column
{
    protected $name;
    protected $type;
    protected $isPrimary = false;

    protected function  __construct()
    {
    }

    /**
     * @return My_DataBase_Mapper_Column
     */
    public static function factory()
    {
        return new self();
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     * @return My_DataBase_Mapper_Column
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }


    /**
     * @param string $type
     * @return My_DataBase_Mapper_Column
     */
    public function setType($type)
    {
        $this->type = $this->sqlTypeToPhpType($type);
        return $this;
    }

    /**
     * @return boolean;
     */
    public function isPrimary()
    {
        return $this->isPrimary;
    }

    /**
     * @return My_DataBase_Mapper_Column
     */
    public function setPrimary($isPrimary = true)
    {
        $this->isPrimary = $isPrimary;
        return $this;
    }

    /**
     * Map a SQL data type to a PHP data type
     * @param string $sqlType
     * @return string
     */
    public function sqlTypeToPhpType($sqlType)
    {
        $phpType = 'string';
        switch (strtoupper($sqlType))
        {
            case 'CHAR':
            case 'VARCHAR':
            case 'LONGVARCHAR':
            case 'TEXT':
            case 'LONGTEXT':
            case 'DECIMAL':
            case 'BIGINT':
            case 'LONGBLOB':
            case 'DATE':
            case 'TIME':
            case 'TIMESTAMP':
                $phpType = 'string';
                My_Logger::getInstance()->info(sprintf('SQL type "%s" mapped to PHP type "%s"', $sqlType, $phpType));
                break;
            case 'TINYINT':
            case 'SMALLINT':
            case 'INTEGER':
            case 'INT':
                $phpType = 'integer';
                My_Logger::getInstance()->info(sprintf('SQL type "%s" mapped to PHP type "%s"', $sqlType, $phpType));
                break;
            case 'BLOB':
            case 'MEDIUMBLOB':
            case 'LONGBLOB':
            case 'REAL':
            case 'FLOAT':
            case 'DOUBLE':
                $phpType = 'double';
                My_Logger::getInstance()->info(sprintf('SQL type "%s" mapped to PHP type "%s"', $sqlType, $phpType));
                break;
            default :
                $phpType = 'string';
                My_Logger::getInstance()->warning(sprintf('SQL type "%s" mapped to PHP type "%s"', $sqlType, $phpType));
                break;
        }
        
        return $phpType;
    }
    
}
?>
